package com.hxht.cmp.module;


import com.hxht.cmp.common.Constants;
import com.hxht.cmp.entity.Court;
import com.hxht.cmp.entity.Trial;
import com.hxht.cmp.entity.User;
import com.hxht.cmp.pojo.CourtroomEx;
import com.hxht.cmp.service.CasePlanService;
import com.hxht.cmp.service.CaseVideoService;
import com.hxht.cmp.service.CourtStatusService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

@Controller
@RequestMapping("/system_monitor")
public class SystemModule {

    @Autowired
    private CourtStatusService courtStatusService;
    @Autowired
    private CasePlanService casePlanService;
    @Autowired
    private CaseVideoService caseVideoService;

    /**
     * 直播页面
     *
     * @param planUid
     * @param vlcCourtUid
     * @param courtRoomUid
     * @param channel
     * @return
     */
    @RequestMapping("/panel_living")
    public ModelAndView toPanelLiving(String planUid, String vlcCourtUid, String courtRoomUid, String channel) {
        ModelAndView modelAndView = new ModelAndView();
        List<Trial> trials = caseVideoService.getTrialsByPlanUid(planUid);
        String trialUid = null;
        for (Trial trial : trials)
            if (Objects.equals(trial.getStatus(), 0) || Objects.equals(trial.getStatus(), 1) || Objects.equals(trial.getStatus(), 5)) {
                trialUid = trial.getUid();
            }
        Map<String, Object> caseMap = casePlanService.getCaseInfoByUid(planUid, trialUid);
        CourtroomEx courtRoom = Constants.COURTROOM_MAP.get(courtRoomUid);
        //将页面中需要的参数携带回去
        modelAndView.addObject("serviceIp", ObjectUtils.isEmpty(courtRoom) ? null : courtRoom.getDeviceIp());
        modelAndView.addObject("courtRoomUid", courtRoomUid);
        modelAndView.addObject("configs", ObjectUtils.isEmpty(courtRoom) ? null : courtRoom.getConfigs());
        modelAndView.addObject("vlcCourtUid", vlcCourtUid);
        modelAndView.addObject("channel", channel);
        modelAndView.addObject("planUid", planUid);
        modelAndView.addObject("caseMap", caseMap);
        modelAndView.setViewName("play/living");
        return modelAndView;
    }

    /**
     * 点播页面
     *
     * @return
     */
    @RequestMapping("/panel_play")
    public ModelAndView panelPlay(String planUid, String trialUid) {
        ModelAndView modelAndView = new ModelAndView();
        if (!ObjectUtils.isEmpty(planUid)) {
            if (!ObjectUtils.isEmpty(trialUid)) {
                Map<String, Object> caseMap = casePlanService.getCaseInfoByUid(planUid, trialUid);
                modelAndView.addObject("caseMap", caseMap);
                modelAndView.addObject("trialUid", trialUid);
                modelAndView.setViewName("play/play");
                return modelAndView;
            }
            List<Trial> trials = caseVideoService.getTrialsByPlanUid(planUid);
            trials.removeIf(tempTrial -> Objects.equals(tempTrial.getStatus(), 1) || Objects.equals(tempTrial.getStatus(), 0));
            if (trials.size() == 1) {
                Map<String, Object> caseMap = casePlanService.getCaseInfoByUid(planUid, trials.get(0).getUid());
                modelAndView.addObject("caseMap", caseMap);
                modelAndView.addObject("trialUid", trials.get(0).getUid());
                modelAndView.setViewName("play/play");
                return modelAndView;
            }
            modelAndView.addObject("trialList", trials);
            modelAndView.addObject("planUid", planUid);
        }
        modelAndView.setViewName("play/play_select");
        return modelAndView;
    }

    /**
     * 法庭状态
     *
     * @param courtUid
     * @return
     */
    @RequestMapping("/sys_court_status")
    public ModelAndView systemCourtStatus(String courtUid) {
        ModelAndView modelAndView = new ModelAndView();
        List<Map<String, Object>> statusList = courtStatusService.selectCourtStatusList(courtUid);
        modelAndView.addObject("statusList", statusList);
        modelAndView.addObject("courtUid", courtUid);
        modelAndView.setViewName("panel/sys_court_status");
        return modelAndView;
    }

    /**
     * 服务器状态
     *
     * @return
     */
    @RequestMapping("/sys_server_status")
    public ModelAndView systemServerStatus() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("panel/sys_server_status");
        return modelAndView;
    }

    /**
     * 法庭设备管理
     *
     * @return
     */
    @RequestMapping("/sys_equipment_management")
    public ModelAndView sysEquipmentManagement(String courtUid) {
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        if (ObjectUtils.isEmpty(courtUid)) {
            User user = (User) session.getAttribute("user");
            courtUid = user.getCourtUid();
        }
        ModelAndView modelAndView = new ModelAndView();
        Court court = Constants.COURT_MAP.get(courtUid);
        List<Court> current_court_list = new ArrayList<>();
        if (court.getLevel() == 1) {
            current_court_list = Constants.COURT_LIST;
        } else {
            for (Court obj : Constants.COURT_LIST) {
                if (obj.getUid().equals(courtUid) || obj.getParentUid().equals(courtUid)) {
                    current_court_list.add(obj);
                }
            }
        }
        modelAndView.addObject("courtUid", courtUid);
        modelAndView.addObject("currentCourtList", current_court_list);
        modelAndView.addObject("courtroomList", Constants.COURTROOM_LIST);
        modelAndView.setViewName("panel/sys_equipment_management");
        return modelAndView;
    }
}
